package vip.meeet.jdbc;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TableInfo {

    public static void getTables() throws SQLException, ClassNotFoundException {
        Connection connection = ConnectionHelper.getConnection();
        String sql = "select distinct TABLE_NAME from tables";
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()){
            System.out.println(rs.getString(1));
        }
    }

    public static List<String> getTables(String tableSchema) throws SQLException, ClassNotFoundException {
        Connection connection = ConnectionHelper.getConnection();
        String sql = "select distinct TABLE_NAME from tables WHERE table_schema = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,tableSchema);
        ResultSet rs = ps.executeQuery();
        ArrayList<String> tables = new ArrayList<>();
        while (rs.next()){
            tables.add(rs.getString(1));
//            System.out.println(rs.getString(1));
        }
        return tables;
    }

    public static void getSystemTables() throws SQLException, ClassNotFoundException {
        Connection connection = ConnectionHelper.getConnection();
        String sql = "select distinct TABLE_NAME from tables WHERE TABLE_TYPE = 'SYSTEM VIEW'";
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()){
            System.out.println(rs.getString(1));
        }
    }

    public static void getUserTables() throws SQLException, ClassNotFoundException {
        Connection connection = ConnectionHelper.getConnection();
        String sql = "select distinct TABLE_NAME from tables WHERE TABLE_TYPE = 'BASE TABLE'";
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()){
            System.out.println(rs.getString(1));
        }
    }



    public static void getDBs() throws SQLException, ClassNotFoundException {
        Connection connection = ConnectionHelper.getConnection();
        String sql = "select distinct SCHEMA_NAME from schemata";
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()){
            System.out.println(rs.getString(1));
        }
    }


}
